package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.entity.Attendance;
import oa.entity.User;
import oa.paging.Page;
import oa.service.impl.AttendanceServiceImpl;
import oa.service.impl.DepartmentServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class PageUserList
 */
@WebServlet("/pageuserlist")
public class PageUserList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PageUserList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();		
		UserServiceImpl user = new UserServiceImpl();
		DepartmentServiceImpl depart = new DepartmentServiceImpl();
		AttendanceServiceImpl attendan = new AttendanceServiceImpl();
		Attendance att = new Attendance();
		User us = new User();
		Map<String, Object> haplist = new HashMap<String, Object>(2);
		List<User>  usershowlist= new ArrayList<User>();
		Page<User> pageInfo = new Page<User>();		
		String opt  = request.getParameter("opt");
		int departid=0;
		String bumen =request.getParameter("departidd");	
		if(bumen==null) {
			bumen = "0" ;
		}else {
			departid = Integer.parseInt(bumen);
		}
		int companyidmoren = 0;
		int count =0;
		String pageno = request.getParameter("PageNo");
		if (pageno == null) {
			pageno = "1";
		}
		
		if("newcompanyid".equals(opt)) {
			departid =  Integer.parseInt(request.getParameter("bumenid"));
			companyidmoren = Integer.parseInt(request.getParameter("gongsiid"));
		}else {
			departid = Integer.parseInt(bumen);
			String id = request.getParameter("companyidmoren");
			if(id==null) {
				id ="0";
			}
			if(id==null || id=="0") {
			  int companyid =(int)session.getAttribute("companyid");
				companyidmoren=companyid;
			}else {
				companyidmoren=Integer.parseInt(id);
			}
		
		}
		
		
		
		
		try {
				   
	    	// 根据员工id到考勤表里进行计算
			us.setCompanyId(companyidmoren);
			us.setDeptId(departid);
			pageInfo.setCurrentPage(Integer.parseInt(pageno));
			pageInfo.setPageSize(6);
			usershowlist = user.selectAllUser(us, pageInfo.getCurrentPage(), pageInfo.getPageSize());
			count = user.selectAllUserCount(departid, companyidmoren);
			pageInfo.setTotalRow(count);
			pageInfo.setPageList(usershowlist);
			session.setAttribute("page", pageInfo);
			session.setAttribute("gongsiid", companyidmoren);
			session.setAttribute("bumenid", departid);
			request.getRequestDispatcher("dept_statistics.jsp").forward(request, response);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
